package com.atguigu.order.controller;

import com.atguigu.commonutils.result.R;
import com.atguigu.order.entity.Order;
import com.atguigu.order.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author kabuda
 * @create 2023-05-18 8:53
 */
@RestController
@RequestMapping("/eduorder")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/createOrder/{courseId}")
    public R createOrder(@PathVariable String courseId, HttpServletRequest request){
        String orderSn=orderService.saveOrder(courseId, request);
        return R.ok().data("orderSn",orderSn);
    }
    @GetMapping("/getOrder/{orderNo}")
    public R getOrder(@PathVariable String orderNo){
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",orderNo);
        Order order = orderService.getOne(wrapper);
        return R.ok().data("order",order);
    }

    /**
     * 调用订单服务，查询当前用户是否购买当前课程
     * @param courseId
     * @param memberId
     * @return
     */
    @GetMapping("/isBuy")
    public boolean isBuy(@RequestParam("courseId") String courseId,@RequestParam("memberId") String memberId){
        return  orderService.isBuy(courseId, memberId);
    }

}
